#include <QPainter>
#include <QMetaType>
#include <QToolTip>
#include <QFileDialog>
#include <QScreen>

#include <QTreeWidget>

#include "qlsavwidget.h"
#include "ui_qlsavwidget.h"
#include "../QLsFunc/qlsfunc.h"

QLsAVWidget::QLsAVWidget(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::QLsAVWidget),
	m_avPlayer(new QLsAVPlayer),
	m_playerOrder(PlayerOrder_Nor),
	m_bFull(false),
	m_clickedTimer(new QTimer(this)),
	m_curTimeTimer(new QTimer(this)),
	m_volumeLabel(new QLabel(this)),
	m_volumeLabelTimer(new QTimer(this)),
	m_controlWidgetTimer(new QTimer(this)),
	m_controlWidgetOpacity(new QGraphicsOpacityEffect(this)),
	m_controlWidgetAnimation(new QPropertyAnimation(this))
{
	ui->setupUi(this);

	/* 界面初始化 */
	HomeInit();
}

QLsAVWidget::~QLsAVWidget()
{
	delete m_avPlayer;
	m_avPlayer = nullptr;

	delete ui;
}

/* 路径 */
QString QLsAVWidget::AVUrl() const
{
	return m_avPlayer->AVUrl();
}

void QLsAVWidget::setAVUrl(const QString &AVUrl)
{
	m_avPlayer->setAVUrl(AVUrl);
}

/* 绘图事件重载 */
void QLsAVWidget::paintEvent(QPaintEvent *)
{
	QPainter painter(this);

	painter.setRenderHint(QPainter::Antialiasing);
	painter.setRenderHint(QPainter::TextAntialiasing);
	painter.setRenderHint(QPainter::SmoothPixmapTransform);
	painter.setRenderHint(QPainter::HighQualityAntialiasing);

	/* 黑色背景 */
	painter.setBrush(Qt::black);
	painter.drawRect(0,0,ui->showWidget->width(),ui->showWidget->height());

	/* 图像校验 */
	if (m_showImage.size().width() <= 0) return;

	/**
	 * 将图像按比例缩放成和窗口一样大小
	 * 这里效率比较低下  还不知道如何优化
	 */
	QImage img = m_showImage.scaled(
				ui->showWidget->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);

	/* 图像居中 */
	int x = (ui->showWidget->width() - img.width()) / 2;
	int y = (ui->showWidget->height() - img.height()) / 2;

	/* 画出图像 */
	painter.drawImage(QPoint(x,y),img);
}

/* 事件过滤器 */
bool QLsAVWidget::eventFilter(QObject *watched, QEvent *event)
{
	/* 显示 */
	if(watched == ui->showWidget)
	{
		/* 鼠标移动 */
		if(event->type() == QEvent::MouseMove)
		{
			m_bMove = true;

			if(ui->showWidget->cursor() == Qt::ArrowCursor)
			{
				if(m_controlWidgetTimer->isActive())
					m_controlWidgetTimer->stop();
				m_controlWidgetTimer->start();
			}
			else
			{
				/* 显示鼠标 */
				ui->showWidget->setCursor(Qt::ArrowCursor);

				/* 显示控制条动画 */
				m_controlWidgetAnimation->setDuration(300);
				m_controlWidgetAnimation->setStartValue(0);
				m_controlWidgetAnimation->setEndValue(1);
				m_controlWidgetAnimation->start();

				if(m_controlWidgetTimer->isActive())
					m_controlWidgetTimer->stop();
				m_controlWidgetTimer->start();
			}

			/* 拖拽窗口 */
			if(m_bPress)
			{
				/* 显示侧边栏按钮跟随 */
				QPoint p = ui->showWidget->mapToGlobal(QPoint(0, 0));
				m_showSidebarBtn->move(
							p.x() + ui->showWidget->width() - 30,
							p.y() + ui->showWidget->height() / 2);
				m_showSidebarBtn->raise();

				/* 音量显示标签跟随 */
				m_volumeLabel->move(p.x() + 20, p.y() + ui->showWidget->height() - 100);
			}
		}

		/* 移入 */
		if(event->type() == QEvent::Enter)
		{
			m_showSidebarBtn->show();
			QPoint p = ui->showWidget->mapToGlobal(QPoint(0, 0));
			m_showSidebarBtn->move(
						p.x() + ui->showWidget->width() - 30,
						p.y() + ui->showWidget->height() / 2);
			m_showSidebarBtn->raise();
		}

		/* 移出 */
		if(event->type() == QEvent::Leave)
		{
			QPoint p = QCursor::pos();
			if(p.x() < m_showSidebarBtn->pos().x() ||
					p.x() > m_showSidebarBtn->pos().x() + m_showSidebarBtn->width() ||
					p.y() < m_showSidebarBtn->pos().y() ||
					p.y() > m_showSidebarBtn->pos().y() + m_showSidebarBtn->height())
				m_showSidebarBtn->hide();
		}

		/* 滚轮：音量调节 */
		if(event->type() == QEvent::Wheel)
		{
			if(m_avPlayer->mute()) return QWidget::eventFilter(watched, event);

			QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
			int val = m_volumeToolTipWidget->value();
			bool bShowToolTip = false;

			/* 上滚，增加5音量 */
			if(wheelEvent->delta() > 0)
			{
				val += 5;
				if(val > 100)
				{
					val = 100;
					bShowToolTip = true;
				}
			}
			/* 下滚，减少5音量 */
			else
			{
				val -= 5;
				if(val < 0)
				{
					val = 0;
					bShowToolTip = true;
				}
			}

			m_volumeToolTipWidget->setValue(val);
			if(bShowToolTip) ShowVolumeToolTip(val);

			event->accept();
			return true;
		}

		/* 鼠标事件 */
		QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
		if(mouseEvent)
		{	/* 右键菜单 */
			if(mouseEvent->button() == Qt::RightButton ||
					mouseEvent->buttons() == Qt::RightButton)
			{
				event->accept();
				return true;
			}

			/* 左键 */
			if(mouseEvent->button() == Qt::LeftButton ||
					mouseEvent->buttons() == Qt::LeftButton)
			{	/* 双击 */
				if(event->type() == QEvent::MouseButtonDblClick)
				{
					/* 触发双击事件则关闭单击事件的定时器 */
					m_clickedTimer->stop();

					on_fullBtn_clicked();
				}

				/* 按下 */
				if(event->type() == QEvent::MouseButtonPress)
				{
					m_bPress = true;

					/* 记录触发单击的鼠标事件，启动单击事件定时器，区分单双击 */
					m_clickedMouseButton = mouseEvent->buttons();
					m_clickedTimer->start(200);
				}

				/* 单击：播放/暂停 */
				if(event->type() == QEvent::MouseButtonRelease)
				{
					m_bPress = false;
					m_bMove = false;
				}
			}
		}

		/* 按键事件 */
		QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
		if(keyEvent)
		{
			switch (keyEvent->key())
			{
			case Qt::Key_Left:		/* 左：后退10s */
				ui->timeHSlider->setValue(ui->timeHSlider->value() - 10);
				event->accept();
				return true;
				break;
			case Qt::Key_Right:		/* 右：前进10s */
				ui->timeHSlider->setValue(ui->timeHSlider->value() + 10);
				event->accept();
				return true;
				break;
			case Qt::Key_Up:		/* 上：增加10%音量 */
			case Qt::Key_Down:		/* 下：减少10%音量 */
			{
				if(event->type() == QEvent::KeyPress)
				{
					/* 重复事件过滤 */
					if(keyEvent->isAutoRepeat() || m_bKeyPress)
						return QWidget::eventFilter(watched, event);

					int val = m_volumeToolTipWidget->value();
					bool bShowToolTip = false;

					if(keyEvent->key() == Qt::Key_Up)
					{
						val += 10;
						if(val > 100)
						{
							val = 100;
							bShowToolTip = true;
						}
					}
					else
					{
						val -= 10;
						if(val > 100)
						{
							val = 100;
							bShowToolTip = true;
						}
					}

					m_volumeToolTipWidget->setValue(val);
					if(bShowToolTip) ShowVolumeToolTip(val);
					m_bKeyPress = true;
				}
				else if(event->type() == QEvent::KeyRelease)
				{	/* 重复事件过滤 */
					if(keyEvent->isAutoRepeat() || !m_bKeyPress)
						return QWidget::eventFilter(watched, event);

					m_bKeyPress = false;
				}
				event->accept();
				return true;
				break;
			}
			default: break;
			}
		}
	}

	/* 声音 */
	if(watched == ui->volumeBtn || watched == m_volumeToolTipWidget)
	{	/* 单击静音按钮时，会触发这个事件，销毁 m_volumeToolTipWidget ，导致后续操作异常 */
		if(watched == m_volumeToolTipWidget && event->type() == QEvent::DeferredDelete)
		{
			event->accept();
			return true;
		}

		/* 移入 */
		if(event->type() == QEvent::Enter)
		{
			if(m_avPlayer->mute()) return QWidget::eventFilter(watched, event);
			m_volumeToolTipWidget->show();
			QPoint p = ui->volumeBtn->mapToGlobal(QPoint(0, 0));
			m_volumeToolTipWidget->move(
						p.x() - (m_volumeToolTipWidget->width() - ui->volumeBtn->width()) / 2,
						p.y() - m_volumeToolTipWidget->height());
		}
		/* 移出 */
		if(event->type() == QEvent::Leave && watched == ui->volumeBtn)
		{
			if(!m_volumeToolTipWidget->isActiveWindow())
				return QWidget::eventFilter(watched, event);
			QPoint p = QCursor::pos();
			if(p.x() < m_volumeToolTipWidget->pos().x() ||
					p.x() > m_volumeToolTipWidget->pos().x() + m_volumeToolTipWidget->width() ||
					p.y() < m_volumeToolTipWidget->pos().y() ||
					p.y() > m_volumeToolTipWidget->pos().y() + m_volumeToolTipWidget->height())
				m_volumeToolTipWidget->hide();
		}
		if(event->type() == QEvent::Leave && watched == m_volumeToolTipWidget)
		{
			if(!m_volumeToolTipWidget->isActiveWindow())
				return QWidget::eventFilter(watched, event);
			QPoint p = QCursor::pos();
			if(p.x() < ui->volumeBtn->pos().x() ||
					p.x() > ui->volumeBtn->pos().x() + ui->volumeBtn->width() ||
					p.y() < ui->volumeBtn->pos().y() ||
					p.y() > ui->volumeBtn->pos().y() + ui->volumeBtn->height())
				m_volumeToolTipWidget->hide();
		}
	}

	return QWidget::eventFilter(watched, event);
}

/* 拖拽事件重载 - 鼠标松开时执行 */
void QLsAVWidget::dropEvent(QDropEvent *event)
{
	/* 判断拖拽进来的类型，取出文件，进行播放 */
	if(event->mimeData()->hasUrls())
	{
		QString url = event->mimeData()->urls().first().toLocalFile();
		m_avPlayer->Stop();
		m_avPlayer->setAVUrl(url);
		m_avPlayer->Load();
	}
	else if(event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
	{
		QTreeWidget *treeWidget = (QTreeWidget *)event->source();
		if(treeWidget != 0)
		{
			QString url = treeWidget->currentItem()->data(0, Qt::UserRole).toString();
			m_avPlayer->Stop();
			m_avPlayer->setAVUrl(url);
			m_avPlayer->Load();
		}
	}
}

/* 拖拽事件重载 - 拖拽进来时执行 */
void QLsAVWidget::dragEnterEvent(QDragEnterEvent *event)
{
	/* 拖曳进来的时候先判断下类型，非法类型则不处理 */
	if(event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
	{
		event->setDropAction(Qt::CopyAction);
		event->accept();
	}
	else if(event->mimeData()->hasFormat("text/uri-list"))
	{
		event->setDropAction(Qt::LinkAction);
		event->accept();
	}
	else event->ignore();
}

/* Sec转Str */
QString QLsAVWidget::SecToTimeStr(qint64 sec)
{
	QString timeStr;

	/* hh:mm:ss */
	if(sec / 3600)
	{
		timeStr = QString("%1:%2:%3").arg(
					sec / 3600, 2, 10, QLatin1Char('0')).arg(
					sec / 60 % 60, 2, 10, QLatin1Char('0')).arg(
					sec % 60, 2, 10, QLatin1Char('0'));
	}
	/* mm:ss */
	else
	{
		timeStr = QString("%1:%2").arg(
					sec / 60 % 60, 2, 10, QLatin1Char('0')).arg(
					sec % 60, 2, 10, QLatin1Char('0'));
	}

	return timeStr;
}

/* 音量同步显示 */
void QLsAVWidget::ShowVolumeToolTip(int volume)
{
	m_volumeLabel->setText(QString("音量：%1%").arg(volume));

	m_volumeLabel->show();
	QPoint p = ui->showWidget->mapToGlobal(QPoint(0, 0));
	m_volumeLabel->move(p.x() + 20, p.y() + ui->showWidget->height() - 100);

	if(m_volumeLabelTimer->isActive())
		m_volumeLabelTimer->stop();
	m_volumeLabelTimer->start();
}

/* 界面初始化 */
void QLsAVWidget::HomeInit()
{
	/* qss */
	m_qssPlay = "icon/form/play-FFFFFF.png";							/* 播放 */
	m_qssPlayHover = "icon/form/play-FF5C38.png";
	m_qssPause = "icon/form/suspended-FFFFFF.png";						/* 暂停 */
	m_qssPauseHover = "icon/form/suspended-FF5C38.png";
	m_qssVolume = "icon/form/sound-filling-FFFFFF.png";					/* 音量 */
	m_qssVolumeHover = "icon/form/sound-filling-FF5C38.png";
	m_qssMute = "icon/form/sound-Mute-FFFFFF.png";						/* 静音 */
	m_qssMuteHover = "icon/form/sound-Mute-FF5C38.png";
	m_qssFullExpand = "icon/form/fullscreen-expand-FFFFFF.png";			/* 全屏-展开 */
	m_qssFullExpandHover = "icon/form/fullscreen-expand-FF5C38.png";
	m_qssFullShrink = "icon/form/fullscreen-shrink-FFFFFF.png";			/* 全屏-收缩 */
	m_qssFullShrinkHover = "icon/form/fullscreen-shrink-FF5C38.png";
	m_qssSidebarRight = "icon/form/arrow-right-bold-FFFFFF.png";		/* 侧边栏-右-收缩 */
	m_qssSidebarRightHover = "icon/form/arrow-right-bold-FF5C38.png";
	m_qssSidebarLeft = "icon/form/arrow-left-bold-FFFFFF.png";			/* 侧边栏-左-展开 */
	m_qssSidebarLeftHover = "icon/form/arrow-left-bold-FF5C38.png";

	/* 播放按钮qss样式 */
	m_playBtnQss_play = QString(
				"QPushButton{"
				"	border-image: url(%1);"
				"}"
				""
				"QPushButton:hover{"
				"	border-image: url(%2);"
				"}").arg(m_qssPlay).arg(m_qssPlayHover);
	m_playBtnQss_pause = QString(
				"QPushButton{"
				"	border-image: url(%1);"
				"}"
				""
				"QPushButton:hover{"
				"	border-image: url(%2);"
				"}").arg(m_qssPause).arg(m_qssPauseHover);

	/* 静音按钮qss样式 */
	m_volumeBtnQss_volume = QString(
				"QPushButton{"
				"	border-image: url(%1);"
				"}"
				""
				"QPushButton:hover{"
				"	border-image: url(%2);"
				"}").arg(m_qssVolume).arg(m_qssVolumeHover);
	m_volumeBtnQss_mute = QString(
				"QPushButton{"
				"	border-image: url(%1);"
				"}"
				""
				"QPushButton:hover{"
				"	border-image: url(%2);"
				"}").arg(m_qssMute).arg(m_qssMuteHover);

	/* 全屏按钮 */
	m_fullBtnQss_full = QString(
				"QPushButton{"
				"	border-image: url(%1);"
				"}"
				""
				"QPushButton:hover{"
				"	border-image: url(%2);"
				"}").arg(m_qssFullExpand).arg(m_qssFullExpandHover);
	m_fullBtnQss_normal = QString(
				"QPushButton{"
				"	border-image: url(%1);"
				"}"
				""
				"QPushButton:hover{"
				"	border-image: url(%2);"
				"}").arg(m_qssFullShrink).arg(m_qssFullShrinkHover);

	/* 显示侧边栏按钮 */
	m_showSidebarBtnQss_right = QString(
				"QPushButton{"
				"	border-image: url(%1);"
				"	background-color: rgba(0,0,0,0);"
				"}"
				""
				"QPushButton:hover{"
				"	border-image: url(%2);"
				"}").arg(m_qssSidebarRight).arg(m_qssSidebarRightHover);
	m_showSidebarBtnQss_left = QString(
				"QPushButton{"
				"	border-image: url(%1);"
				"	background-color: rgba(0,0,0,0);"
				"}"
				""
				"QPushButton:hover{"
				"	border-image: url(%2);"
				"}").arg(m_qssSidebarLeft).arg(m_qssSidebarLeftHover);

	setAcceptDrops(true);		/* 支持拖放 */

	/* ui->showWidget */
	ui->showWidget->installEventFilter(this);
	/**
	 * 焦点切换方式改为Tab键和鼠标点击
	 * 防止 上下按键 切换控件焦点
	 * 让事件拦截器获取到上下按键事件，进行拦截，并改为音量调节功能
	 */
	ui->showWidget->setFocusPolicy(Qt::StrongFocus);
	connect(m_clickedTimer, &QTimer::timeout, this, &QLsAVWidget::slotClicked);

	/* 进度条 */
	connect(ui->timeHSlider, &QLsProgressBar::valueChanged, this, &QLsAVWidget::slotCurTimeChange);
	connect(ui->timeHSlider, &QLsProgressBar::sigSeek, [=](qint64 uSec)
	{	/* 视频跳转 */
		m_avPlayer->Seek(uSec);
		ui->curTimeLabel->setText(SecToTimeStr(uSec / 1000000));
	});
	m_curTimeTimer->setInterval(300);
	connect(m_curTimeTimer, &QTimer::timeout, [=]()
	{	/* 获取视频进度(当前时间) */
		qint64 sec = m_avPlayer->curTime();
		ui->curTimeLabel->setText(SecToTimeStr(sec));
		disconnect(ui->timeHSlider, &QSlider::valueChanged, this, &QLsAVWidget::slotCurTimeChange);
		ui->timeHSlider->setValue(sec);
		connect(ui->timeHSlider, &QSlider::valueChanged, this, &QLsAVWidget::slotCurTimeChange);
	});

	/* 音量条 */
	ui->volumeBtn->setStyleSheet(m_volumeBtnQss_volume);
	ui->volumeBtn->installEventFilter(this);
	/* 悬浮音量条 */
	m_volumeToolTipWidget = new QLsVolumeToolTipWidget(ui->showWidget);
	m_volumeToolTipWidget->installEventFilter(this);
	connect(m_volumeToolTipWidget, &QLsVolumeToolTipWidget::sigVolumeChange,
			this, &QLsAVWidget::slotVolumeChange);

	m_volumeLabel->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
	m_volumeLabel->setFixedSize(100, 30);
	m_volumeLabel->setStyleSheet(
				"QLabel{"
				"	 background-color: rgba(0,0,0,0);"
				"	 color: white;"
				"}");
	m_volumeLabel->hide();

	m_volumeLabelTimer->setInterval(3000);
	connect(m_volumeLabelTimer, &QTimer::timeout, m_volumeLabel, &QLabel::hide);

	/* 控制栏 */
	/* 初始化透明度效果 */
	m_controlWidgetOpacity->setOpacity(1);
	ui->controlWidget->setGraphicsEffect(m_controlWidgetOpacity);
	/* 定时器5s无操作自动隐藏 */
	m_controlWidgetTimer->setInterval(5000);
	m_controlWidgetAnimation->setTargetObject(m_controlWidgetOpacity);
	m_controlWidgetAnimation->setPropertyName("opacity");
	connect(m_controlWidgetTimer, &QTimer::timeout, [=]()
	{	/* 隐藏鼠标 */
		ui->showWidget->setCursor(Qt::BlankCursor);

		/* 隐藏控制栏动画 */
		m_controlWidgetAnimation->setDuration(800);
		m_controlWidgetAnimation->setStartValue(1);
		m_controlWidgetAnimation->setEndValue(0);
		m_controlWidgetAnimation->start();
	});
	ui->playBtn->setStyleSheet(m_playBtnQss_play);
	ui->fullBtn->setStyleSheet(m_fullBtnQss_full);

	/* 侧边栏 */
	m_showSidebarBtn = new QPushButton(ui->showWidget);
	m_showSidebarBtn->setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
	m_showSidebarBtn->setFixedSize(20, 20);
	m_showSidebarBtn->setStyleSheet(m_showSidebarBtnQss_right);
	m_showSidebarBtn->hide();
	connect(m_showSidebarBtn, &QPushButton::clicked, this, &QLsAVWidget::slotShowSidebarBtnClicked);

	connect(m_avPlayer, &QLsAVPlayer::sigGetOneFrame, this, &QLsAVWidget::slotGetOneFrame);
	connect(m_avPlayer, &QLsAVPlayer::sigTotalTimeChange, this, &QLsAVWidget::slotTotalTimeChange);
	qRegisterMetaType<QLsAVPlayer::QLsPlayerStatus>("QLsPlayerStatus");
	connect(m_avPlayer, &QLsAVPlayer::sigStatusChange, this, &QLsAVWidget::slotStatusChange);
}

/* 静音 */
void QLsAVWidget::on_volumeBtn_clicked()
{
	/* 取消静音 */
	if(m_avPlayer->mute())
	{
		m_avPlayer->setMute(false);
		ui->volumeBtn->setStyleSheet(m_volumeBtnQss_volume);
	}
	/* 静音 */
	else
	{
		m_avPlayer->setMute(true);
		ui->volumeBtn->setStyleSheet(m_volumeBtnQss_mute);
	}
}

/* 播放/暂停 */
void QLsAVWidget::on_playBtn_clicked()
{
	switch(m_avPlayer->playerStatus())
	{
	case QLsAVPlayer::Player_Default:
	case QLsAVPlayer::Player_Stop:
		m_avPlayer->Load();
		break;
	case QLsAVPlayer::Player_Playing:
		m_avPlayer->Pause();
		break;
	case QLsAVPlayer::Player_Pause:
		m_avPlayer->Play();
		break;
	default:
		break;
	}
}

/* 获取一帧图像 的槽函数 */
void QLsAVWidget::slotGetOneFrame(QImage img)
{
	m_showImage = img;
	update();
}

/* 视频总时长变化 的槽函数 */
void QLsAVWidget::slotTotalTimeChange(qint64 uSec)
{
	qint64 sec = uSec / 1000000;
	ui->timeHSlider->setRange(0, sec);
	ui->totalTimeLabel->setText(SecToTimeStr(sec));
}

/* 当前时间变化 的槽函数 */
void QLsAVWidget::slotCurTimeChange(int sec)
{
	m_avPlayer->Seek(sec * 1000000);
	ui->curTimeLabel->setText(SecToTimeStr(sec));
}

/* 状态变化 的信号 */
void QLsAVWidget::slotStatusChange(QLsAVPlayer::QLsPlayerStatus status)
{
	switch(status)
	{
	case QLsAVPlayer::Player_Default:
		ui->playBtn->setStyleSheet(m_playBtnQss_play);
		ui->openBtn->setVisible(true);
		m_curTimeTimer->stop();
		break;
	case QLsAVPlayer::Player_Playing:
		ui->playBtn->setStyleSheet(m_playBtnQss_pause);
		ui->openBtn->setVisible(false);
		m_curTimeTimer->start();
		break;
	case QLsAVPlayer::Player_Pause:
		ui->playBtn->setStyleSheet(m_playBtnQss_play);
		m_curTimeTimer->stop();
		break;
	case QLsAVPlayer::Player_Stop:
		ui->playBtn->setStyleSheet(m_playBtnQss_play);
		m_curTimeTimer->stop();
		/* 播放模式 */
		switch (m_playerOrder)
		{
		case PlayerOrder_SingleCycle:		/* 单循环 */
			/* 自动重新播放 */
			m_avPlayer->setPreview(false);
			m_avPlayer->Replay();
			break;
		case PlayerOrder_OrderCycle:		/* 顺序循环 */
			/* 按播放列表顺序自动播放 */
			/** TODO:暂时没加播放列表，先单循环 */
			m_avPlayer->setPreview(false);
			m_avPlayer->Replay();
			break;
		case PlayerOrder_Nor:				/* 不循环 */
			/* 弹出选择本地视频按钮 */
			m_avPlayer->setPreview(true);
			ui->openBtn->setVisible(true);
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}
}

/* 全屏 */
void QLsAVWidget::on_fullBtn_clicked()
{
	/* 正常 */
	if(m_bFull)
	{
		/* 显示父窗口 */
		QWidget *obj = this;
		while(obj->parent()) obj = qobject_cast<QWidget *>(obj->parent());
		obj->show();

		/* 还原 */
		setWindowFlags(Qt::SubWindow);
		showNormal();
		ui->fullBtn->setStyleSheet(m_fullBtnQss_full);
		m_bFull = false;
	}
	/* 全屏 */
	else
	{
		/* 隐藏父窗口 */
		QWidget *obj = this;
		while(obj->parent()) obj = qobject_cast<QWidget *>(obj->parent());
		obj->hide();

		/* 隐藏侧边栏 */
		if(!ui->infoListWidget->isHidden())
			slotShowSidebarBtnClicked();

		/* 在鼠标所在的显示屏上全屏 */
		setWindowFlags(Qt::Window);
		show();
		/* 将窗口移动到鼠标所在的显示屏，并居中显示 */
		QLsFunc::MoveWidgetToScreenWhereMouse(this);
		/* 全屏 */
		showFullScreen();
		ui->fullBtn->setStyleSheet(m_fullBtnQss_normal);
		m_bFull = true;
	}
}

/* 打开本地视频 */
void QLsAVWidget::on_openBtn_clicked()
{
	QString s = QFileDialog::getOpenFileName(
			   this, "选择要播放的文件",
				".",//初始目录
				"视频文件 (*.flv *.rmvb *.avi *.MP4);; 所有文件 (*.*);; ");
	if (!s.isEmpty())
	{
		s.replace("/","\\");
		m_avPlayer->setAVUrl(s);
		m_avPlayer->Load();
	}
}

/* 音量变化 [0, 1] */
void QLsAVWidget::slotVolumeChange(float volume)
{
	if(volume == 0)
		ui->volumeBtn->setStyleSheet(m_volumeBtnQss_mute);
	else ui->volumeBtn->setStyleSheet(m_volumeBtnQss_volume);

	m_avPlayer->SetVolume(volume);
	ShowVolumeToolTip(qvariant_cast<int>(volume*100));
}

/* 显示/隐藏 侧边栏 */
void QLsAVWidget::slotShowSidebarBtnClicked()
{
	/* 显示 */
	if(ui->infoListWidget->isHidden())
	{
		ui->infoListWidget->show();
		m_showSidebarBtn->setStyleSheet(m_showSidebarBtnQss_right);
		QPoint p = ui->showWidget->mapToGlobal(QPoint(0, 0));
		QTimer::singleShot(0, [=]()
		{
			m_showSidebarBtn->move(
						p.x() + ui->showWidget->width() - 30,
						p.y() + ui->showWidget->height() / 2);
			m_showSidebarBtn->raise();
		});
	}
	/* 隐藏 */
	else
	{
		ui->infoListWidget->hide();
		m_showSidebarBtn->setStyleSheet(m_showSidebarBtnQss_left);
		QPoint p = ui->showWidget->mapToGlobal(QPoint(0, 0));
		QTimer::singleShot(0, [=]()
		{
			m_showSidebarBtn->move(
						p.x() + ui->showWidget->width() - 30,
						p.y() + ui->showWidget->height() / 2);
			m_showSidebarBtn->raise();
		});
	}
}

/* 单击事件 的槽函数 */
void QLsAVWidget::slotClicked()
{
	/* 停止单击事件的定时器 */
	m_clickedTimer->stop();

	if(!m_bMove && !m_bPress)
	{
		on_playBtn_clicked();
	}
}

QLsAVWidget::PlayerOrder QLsAVWidget::playerOrder() const
{
	return m_playerOrder;
}

void QLsAVWidget::setPlayerOrder(const PlayerOrder &playerOrder)
{
	m_playerOrder = playerOrder;

	switch (m_playerOrder)
	{
	case PlayerOrder_SingleCycle:		/* 单循环 */
		/* 自动重新播放 */
		m_avPlayer->setPreview(false);
		m_avPlayer->Replay();
		break;
	case PlayerOrder_OrderCycle:		/* 顺序循环 */
		/* 按播放列表顺序自动播放 */
		/** 暂时没加播放列表，先单循环 */
		m_avPlayer->setPreview(false);
		m_avPlayer->Replay();
		break;
	case PlayerOrder_Nor:		/* 不循环 */
		/* 弹出选择本地视频按钮 */
		m_avPlayer->setPreview(true);
		ui->openBtn->setVisible(true);
		break;
	default:
		break;
	}
}
